#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Git Hooks 动态配置系统 - 简单测试脚本
"""

import sys
import os
from pathlib import Path

# 添加项目根目录到 Python 路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

def main():
    """简单测试"""
    print("Git Hooks 动态配置系统 - 简单测试")
    print("=" * 40)
    
    # 1. 测试依赖
    print("1. 测试依赖...")
    try:
        import yaml
        print("   [成功] PyYAML 可用")
    except ImportError:
        print("   [错误] PyYAML 未安装，请运行: pip install PyYAML")
        return False
    
    # 2. 测试配置文件
    print("2. 测试配置文件...")
    config_file = project_root / "config" / "hooks.yaml"
    if config_file.exists():
        try:
            with open(config_file, 'r', encoding='utf-8') as f:
                config = yaml.safe_load(f)
            print("   [成功] 配置文件解析成功")
        except Exception as e:
            print(f"   [错误] 配置文件解析失败: {e}")
            return False
    else:
        print("   [错误] 配置文件不存在")
        return False
    
    # 3. 测试核心模块
    print("3. 测试核心模块...")
    try:
        from core.config_parser import ConfigParser
        parser = ConfigParser("config")
        global_config = parser.get_global_config()
        if global_config:
            print("   [成功] 配置解析器工作正常")
        else:
            print("   [错误] 配置解析器无法获取全局配置")
            return False
    except Exception as e:
        print(f"   [错误] 配置解析器测试失败: {e}")
        return False
    
    # 4. 测试任务执行器
    print("4. 测试任务执行器...")
    try:
        from core.task_executor import TaskExecutor, TaskResult, TaskStatus
        from core.config_parser import TaskConfig, TaskType
        
        executor = TaskExecutor()
        
        # 创建一个简单的测试任务
        test_task = TaskConfig(
            name="测试任务",
            type=TaskType.COMMIT_MSG_VALIDATOR,
            config={
                'pattern': '^test:',
                'min_length': 5,
                'max_length': 50
            }
        )
        
        context = {'commit_msg': 'test: 这是测试'}
        result = executor.execute_task(test_task, context)
        
        if result.status == TaskStatus.SUCCESS:
            print("   [成功] 任务执行器工作正常")
        else:
            print(f"   [错误] 任务执行失败: {result.error}")
            return False
    except Exception as e:
        print(f"   [错误] 任务执行器测试失败: {e}")
        return False
    
    # 5. 测试文件结构
    print("5. 测试文件结构...")
    required_files = [
        "config/hooks.yaml",
        "core/config_parser.py",
        "core/task_executor.py",
        "core/hook_manager.py",
        "tools/install.py",
        "tools/config.py"
    ]
    
    missing_files = []
    for file_path in required_files:
        if not (project_root / file_path).exists():
            missing_files.append(file_path)
    
    if missing_files:
        print(f"   [错误] 缺少文件: {missing_files}")
        return False
    else:
        print("   [成功] 所有核心文件都存在")
    
    print("\n" + "=" * 40)
    print("[成功] 基础测试通过！系统可以正常使用。")
    print("\n使用说明:")
    print("1. 安装依赖: pip install PyYAML")
    print("2. 安装系统: python tools/install.py --install")
    print("3. 查看配置: python tools/config.py show")
    print("4. 验证配置: python tools/config.py validate")
    
    return True

if __name__ == "__main__":
    success = main()
    if not success:
        print("\n[错误] 测试失败，请检查相关问题。")
        sys.exit(1)
    else:
        sys.exit(0)
